<template>
  <div class="roles">
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-button type="primary" plain>添加角色</el-button>
    <el-table v-loading="loading" class="mt-15 mb-15" :data="tableData" border style="width: 100%">
      <el-table-column type="expand">
        <template slot-scope="scope">
          <el-row v-for="firstChildren in scope.row.children" :key="firstChildren.id">
            <el-col :span="4">
              <el-tag
                closable
                @close="handleClose(scope.row.id,firstChildren.id,scope.row)"
              >{{firstChildren.authName}}</el-tag>
              <i class="el-icon-arrow-right" v-if="firstChildren.children.length>0"></i>
            </el-col>
            <el-col :span="20">
              <el-row v-for="secondChildren in firstChildren.children" :key="secondChildren.id">
                <el-col :span="4">
                  <el-tag
                    closable
                    type="success"
                    @close="handleClose(scope.row.id,secondChildren.id,scope.row)"
                  >{{secondChildren.authName}}</el-tag>
                  <i class="el-icon-arrow-right" v-if="secondChildren.children.length>0"></i>
                </el-col>
                <el-col :span="20">
                  <el-tag
                    closable
                    type="warning"
                    v-for="lastchildren in secondChildren.children"
                    :key="lastchildren.id"
                    @close="handleClose(scope.row.id,lastchildren.id,scope.row)"
                  >{{lastchildren.authName}}</el-tag>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
          <el-row>
            <el-col>
              <el-tag type="info" v-if="scope.row.children.length<=0">该角色还未授权，请前往授权！</el-tag>
            </el-col>
          </el-row>
        </template>
      </el-table-column>
      <el-table-column type="index" width="50"></el-table-column>
      <el-table-column prop="roleName" label="角色名称" width="180"></el-table-column>
      <el-table-column prop="roleDesc" label="描述"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <!-- 4.1 点击编辑按钮 -->
          <el-button
            circle
            type="primary"
            icon="el-icon-edit"
            @click="handleEdit(scope.row)"
            size="mini"
          ></el-button>
          <el-button
            type="warning"
            @click="handleCheck(scope.row)"
            circle
            icon="el-icon-check"
            size="mini"
          ></el-button>
          <!-- scope.row获取到的是你点击的那一行对象的所有数据 -->
          <el-button
            circle
            icon="el-icon-delete"
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 授权角色对话框 -->
    <el-dialog title="授权角色" :visible.sync="givedialogVisible">
      <div class="tree-box">
        <el-tree
          ref="tree"
          :data="rightList"
          show-checkbox
          node-key="id"
          default-expand-all
          :default-checked-keys="selectIds"
          :props="defaultProps"
        ></el-tree>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="givedialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="isRight">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getRoleList, deleteROlePer, roleRight } from "@/api";
export default {
  data() {
    return {
      tableData: [],
      loading: true,
      givedialogVisible: false,
      rightList: [],
      currentRole: {},
      selectIds: [],
      defaultProps: {
        children: "children",
        label: "authName"
      }
    };
  },
  created() {
    this.initPermissionList();
  },
  methods: {
    handleEdit(row) {},
    handleCheck(row) {
      console.log(row)
      this.currentRole = row;
      this.givedialogVisible = true;
      this.selectIds = [];
      getRoleList("tree").then(res => {
        this.rightList = res.data.data;
      });

      row.children.forEach(firstChildItem => {
        if (firstChildItem.children && firstChildItem.children.length > 0) {
          firstChildItem.children.forEach(secondChildItem => {
            if (
              secondChildItem.children &&
              secondChildItem.children.length > 0
            ) {
              secondChildItem.children.forEach(lastItem => {
                this.selectIds.push(lastItem.id);
              });
            }
          });
        }
      });
    },
    isRight() {
      let allgetCheckedKeys = this.$refs.tree.getCheckedKeys()
      let getHalfCheckedKeys = this.$refs.tree.getHalfCheckedKeys()
      let idsArr = [...allgetCheckedKeys, ...getHalfCheckedKeys];
      console.log(idsArr)
      // roleRight(this.currentRole.id, idsArr.join(",")).then(res => {
      //   if (res.data.meta.status == 200) {
      //     this.$message.success(res.data.meta.msg);
      //     this.givedialogVisible = false;
      //     this.initPermissionList();
      //   } else {
      //     this.$message.error(res.data.meta.msg);
      //   }
      // });
    },
    handleDelete() {},
    initPermissionList() {
      this.loading = true;
      getRoleList().then(res => {
        this.tableData = res.data.data;
        this.loading = false;
      });
    },
    handleClose(roleId, rightId, row) {
      deleteROlePer(roleId, rightId).then(res => {
        if (res.data.meta.status === 200) {
          this.$message.success(res.data.meta.msg);
          row.children = res.data.data;
        }
      });
    }
  }
};
</script>roles
<style lang="scss" scoped>
.roles {
  .el-tag {
    margin: 5px 5px 5px 0;
  }
  .tree-box {
    height: 300px;
    overflow: auto;
  }
}
</style>


